<template>
  <div>
    <!-- 标题文字 -->
    <div class="header">
      <h1>易飨宴厨师上门运营管理平台</h1>
    </div>
    <!-- 登录表单 -->
    <div class="login-form">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
        <el-form-item label="" prop="username">
          <el-input v-model="ruleForm.username" style="font-size: 16px;">
            <template slot="prepend"><i class="el-icon-user-solid"></i></template>
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input v-model="ruleForm.password" style="font-size: 16px;">
            <template slot="prepend"><i class="el-icon-s-ticket"></i></template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%; font-size: 16px;" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 版权信息 -->
    <div class="copyright">
      <p>Copyright &copy; 翻斗大街 - 翻斗花园 - 2号楼1001室</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单
      ruleForm: {
        username: 'root',
        password: '123456'
      },
      // 表单规则
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9523/account/users/login';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios.post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              let loginResult = jsonResult.data;
              localStorage.setItem('localJwt', loginResult.token);
              localStorage.setItem('currentUserId', loginResult.id);
              localStorage.setItem('currentUserName', loginResult.username);
              localStorage.setItem('currentUserAvatar', loginResult.avatar);
              this.$router.push('/');
            } else if (jsonResult.state == 40100) {
              this.$message.error(jsonResult.message);
            } else {
              let title = '操作失败';
              this.$alert(jsonResult.message, title, {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
body {
  height: 100vh;
  background-image: linear-gradient(to bottom left, #5abae8, #3c4bd2);
  background-repeat: no-repeat;
}

.header {
  width: 600px;
  margin: 60px auto 20px auto;
}

.header h1 {
  text-align: center;
  margin: 30px auto;
  font-size: 32px;
  color: #fff;
}

.login-form {
  background: #fff;
  width: 360px;
  margin: 20px auto;
  padding: 50px 50px 20px 50px;
  border-radius: 10px;
}

.copyright {
  width: 600px;
  margin: 30px auto 30px auto;
}

.copyright p {
  text-align: center;
  font-size: 12px;
  color: #f2f4f9;
}
</style>